package com.example.goods.controller;

import com.example.common.api.ResultUtil;
import com.example.common.validator.Insert;
import com.example.common.validator.Update;
import com.example.goods.model.vo.GoodsClassifyVO;
import com.example.goods.model.vo.GoodsVO;
import com.example.goods.service.IGoodsClassifyService;
import com.example.goods.service.IGoodsService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/api/goods")
@Validated
public class GoodsController {

    @Resource
    private IGoodsClassifyService goodsClassifyService;

    @Resource
    private IGoodsService goodsService;

    /**
     * 创建商品分类
     * */
    @RequestMapping(value = "/create_goods_classify", method = RequestMethod.PUT)
    @ResponseBody
    public ResultUtil createGoodsClassify(@RequestBody @Validated(Insert.class) GoodsClassifyVO goodsClassifyVO){
        return goodsClassifyService.createGoodsClassify(goodsClassifyVO);
    }

    /**
     * 根据商品分类ID获取商品分类
     * */
    @RequestMapping(value = "/get_goods_classify/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultUtil getGoodsClassify(@PathVariable(value = "id", required = false) Long id){
        return goodsClassifyService.getGoodsClassify(id);
    }

    /**
     * 新建商品
     * */
    @RequestMapping(value = "/create_goods", method = RequestMethod.PUT)
    @ResponseBody
    public ResultUtil createGoods(@RequestBody @Validated(Insert.class) GoodsVO goodsVO){
        return goodsService.createGoods(goodsVO);
    }

    /**
     * 根据ID修改商品
     * */
    @RequestMapping(value = "/update_goods_by_id", method = RequestMethod.POST)
    @ResponseBody
    public ResultUtil updateGoodsById(@RequestBody @Validated(Update.class) GoodsVO goodsVO){
        return goodsService.updateGoodsById(goodsVO);
    }

    /**
     * 分页获取商品
     * */
    @RequestMapping(value = "/get_goods_by_page", method = RequestMethod.GET)
    @ResponseBody
    public ResultUtil getGoodsByPage(@RequestParam int pageIndex,
                                     @RequestParam int pageSize,
                                     @RequestBody GoodsVO goodsVO){
        return goodsService.getGoodsByPage(pageIndex, pageSize, goodsVO);
    }

    /**
     * 根据商品类型名称获取商品
     * */
    @RequestMapping(value = "/get_goods_by_page/{classify_name}", method = RequestMethod.GET)
    @ResponseBody
    public ResultUtil getGoodsByClassify(@RequestParam int pageIndex,
                                         @RequestParam int pageSize,
                                         @PathVariable(value = "classify_name", required = false) String classifyName){
        return goodsService.getGoodsByClassify(pageIndex, pageSize, classifyName);
    }

    /**
     * 根据商品类型名称获取商品
     * */
    @RequestMapping(value = "/get_goods_by_id", method = RequestMethod.GET)
    @ResponseBody
    public ResultUtil getGoodsById(@NotNull(message = "商品ID不能为空") @RequestParam Long id){
        return goodsService.getGoodsById(id);
    }
}
